package com.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.entity.Comment;
import com.entity.Task;
import com.entity.Trend;
import com.entity.TrendUserProject;
import com.entity.User;
import com.entity.UserProject;
import com.service.CommentService;
import com.service.TaskService;
import com.service.TrendService;
import com.service.TupService;
import com.service.UserService;
import com.util.DateHelp;
import com.util.PageHelp;
import com.util.SplitHelp;
import com.util.StringHelp;
import com.util.UUIDHelp;

public class TrendServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private TrendService ts=new TrendService();
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session=request.getSession();
		UserProject up=(UserProject) session.getAttribute("up");
		
		String method=request.getParameter("m");
		String hm=request.getMethod();
		new StringHelp();
		if(StringHelp.isEmpty(method)){
			list(request,response,up);
		}else if("say".equals(method)){
			if("POST".endsWith(hm)){
				say(request,response,up);
			}
		}else if("trend".equals(method)){
			if("POST".endsWith(hm)){
				addTrend(request,response,up);
			}
			
		}else if("list2".equals(method)){
			list2(request,response);
		}
	}
	private void list2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		/*String trendid=request.getParameter("id");
		List<Comment> CommentList=ts.findComment(trendid);
		
		request.setAttribute("CommentList", CommentList);
		request.getRequestDispatcher("/WEB-INF/pms/project.jsp").forward(request, response);*/
		
	}
	private void addTrend(HttpServletRequest request,
			HttpServletResponse response, UserProject up) throws IOException {

		String trend=request.getParameter("content");
		if(trend==null||"".equals(trend)){
			response.sendRedirect("trend.jspx?code=10001");
		}else{
			Trend t=new Trend();
			String id=UUIDHelp.executeUUID();
			t.setId(id);
			t.setContent(trend);
			t.setCreateTime(DateHelp.getTime());
			t.setUserid(up.getUser().getId());
			t.setProjectid(up.getProjectid());
			
			t.setUser(up.getUser());
			ts.saveTrend(t);
			String[] str=new SplitHelp().searchsName(trend);
			if(str!=null){
				for(String name:str){
					if(name!=null){
						UserService ud=new UserService();
						User user=ud.findByName(name);
						if(user!=null){
							TupService tups=new TupService();
							TrendUserProject tup=new TrendUserProject();
							tup.setProjectid(up.getProjectid());
							tup.setTrendid(t.getId());
							tup.setUserid(user.getId());
							tup.setTrend(t);
							tup.setUser(user);
							tups.save(tup);
						}
					}else{
						break;
					}
				}
			}
			
			
			response.sendRedirect("trend.jspx");
		}
		
		
	}
	private void say(HttpServletRequest request, HttpServletResponse response, UserProject up) throws IOException {
		String message=request.getParameter("message");
		String id=request.getParameter("id");
		
		Comment c=new Comment();
		c.setCreateTime(DateHelp.getTime());
		c.setId(UUIDHelp.executeUUID());
		c.setMessage(message);
		c.setTrendid(id);
		c.setUserid(up.getUserid());
		ts.saveFromTrend(c);
		
		response.sendRedirect("trend.jspx");
	}
	private void list(HttpServletRequest request, HttpServletResponse response, UserProject up) throws ServletException, IOException {
		String p=request.getParameter("p");
		int pageNum=0;
		if(StringHelp.isEmpty(p)){
			pageNum=1;
		}else {
			try {
				pageNum = Integer.parseInt(p);
			} catch (NumberFormatException e) {
				pageNum=1;
			}
		}
		PageHelp<Trend> page=ts.findTrendByPage(pageNum);
		request.setAttribute("page", page);
		HttpSession session=request.getSession();
		
		String iid=up.getUserid();
		TupService tups=new TupService();
		List<TrendUserProject> trendidList=tups.findBy(iid);
		
		
		TaskService task=new TaskService();
		List<Task> taskList=task.findTaskByUserId(up.getUserid());
		
		request.setAttribute("taskList", taskList);
		CommentService cs=new CommentService();
		
		
		
		request.setAttribute("trendidList", trendidList);
		List<Comment> commentList=cs.findAll();
		
		request.setAttribute("commentList", commentList);
		
		session.setAttribute("up", up);
		request.getRequestDispatcher("/WEB-INF/pms/project.jsp").forward(request, response);
	}
}
